#include "MainWindow.h"
#include "ui_MainWindow.h"
#include "AACEncodeThread.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_openBtn_clicked() {

}


void MainWindow::on_playBtn_clicked() {
    if (this->audioPlayThread == nullptr) {
        FileModel file = FilePicker().pickFile();
        this->audioPlayThread = new AudioPlayThread();
        this->audioPlayThread->fileModel = file;
        this->audioPlayThread->start();
    } else {
        this->audioPlayThread->requestInterruption();
        this->audioPlayThread = nullptr;
    }
}

void MainWindow::on_soundRecordBtn_clicked() {
    if (this->audioRecord == nullptr) {
        this->audioRecord = new AudioRecordThread();
        connect(this->audioRecord, &AudioRecordThread::recordState, this, [this](AudioRecordState state) {
            switch (state) {
            case AudioRecordEnd: {
                this->ui->soundRecordBtn->setText("sound recod");
            } break;

            case AudioRecordFail: {
                this->ui->soundRecordBtn->setText("sound recod");
                qDebug() << "录音失败";
            } break;

            default: break;
            }
        });
        connect(this->audioRecord, &AudioRecordThread::recordDuration, this,[this](int duration) {
            ui->soundRecordBtn->setText(QString::number(duration/60).append(":").append(QString::number(duration%60)));
        });
        this->audioRecord->start();
    } else {
        this->audioRecord->requestInterruption();
        this->audioRecord = nullptr;
    }
}


void MainWindow::on_screenRecordBtn_clicked() {
    if (this->videoRecord == nullptr) {
        this->videoRecord = new VideoRecordTool();
        connect(this->videoRecord, &VideoRecordTool::recordState, this, [this](VideoRecordState state, int duration) {
            switch (state) {
            case VideoRecording:
            {
                ui->screenRecordBtn->setText(QString::number(duration/60).append(":").append(QString::number(duration%60)));
            }
            break;

            case VideoRecordEnd:
            {
                this->ui->screenRecordBtn->setText("screen recod");
            }
            break;

            case VideoRecordFail:
            {
                this->ui->screenRecordBtn->setText("screen recod");
                qDebug() << "录屏失败";
            }
            break;

            default:
                break;
            }
        });
    }

    if (this->videoRecord->isRecording == false) {
        this->videoRecord->startRecord();
    }else {
        this->videoRecord->stopRecord();
    }
}

